package com.itheima.myshopweb.controller;

import com.itheima.myshopuser.pojo.User;
import com.itheima.myshopweb.client.UserClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @File: WebController
 * @Brief: 购票控制类
 * @Details:
 * @Author: Kevin
 * @Date: 2024/10/22 16:35
 * @Version: 1.0
 * @Department: 人工智能与仿真实验室
 * @Copyright: Copyright By AI&Simulation Lab, All Rights Reserved
 */
@RequestMapping("/web")
@RestController
@Component
public class WebController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
//    @Autowired
//    private LoadBalancerClient client;
    @Resource
    private UserClient userClient;

    // 购票方法（原有方案）
//    @RequestMapping(value="/simple-order", method = RequestMethod.GET)
//    public String simpleOrder(){
//        Integer id = 1;
//        User user = restTemplate.getForObject("http://localhost:9101/user/" + id, User.class);
//        System.out.println(user.getUsername() + "==正在购票...");
//        return "购票成功";
//    }

    // 购票方法 Eureka 版本
//    @RequestMapping(value="/order", method = RequestMethod.GET)
//    public String order(){
//        Random r = new Random();
//        Integer id = r.nextInt(16) + 1;
//
//        List<ServiceInstance> instances = discoveryClient.getInstances("MYSHOP-USER");
//        if (instances.size() == 0){
//            System.out.println("没有实例，无法进行处理，强行退出");
//            return "处理失败！";
//        }
//        System.out.println(instances.size());
//        ServiceInstance serviceInstance = instances.get(0);
//        // 请注意：IP + port 会报错，根据新版博客的要求，现修改 url 如下：
//        System.out.println(serviceInstance.getInstanceId());
//        String url = "http://myshop-user/user/" + id;
//        User user = restTemplate.getForObject(url, User.class);
//        System.out.println(user.toString() + "==正在购票...");
//        return "购票成功";
//    }

    // 购票方法 RestTemplate + Ribbon
//    @RequestMapping(value="/order", method = RequestMethod.GET)
//    public String order(){
//        Random r = new Random();
//        Integer id = r.nextInt(16) + 1; // 随机获取用户的 id
//        // 2 号 id 的人被删除了，所以需要更改
//        if (id == 2){
//            id = 1;
//        }
//        // ServiceInstance serviceInstance = client.choose("myshop-user");
//        // String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/" + id;
//        String url = "http://myshop-user/user/" + id;
//        System.out.println(url);
//        User user = restTemplate.getForObject(url, User.class);
//        System.out.println(user + "==正在购票...");
//        return "购票成功";
//    }

    // 购票方法 OpenFeign + Ribbon
    private static final Log log = LogFactory.getLog(WebController.class);
    @ApiOperation(value = "远程方法，根据用户 ID 查询用户的方法")
    @RequestMapping(value="/order", method = RequestMethod.GET)
    public String order(){
        log.info("开始调用order方法...");
        Integer id = 1;
        User user = userClient.findById(id);
        System.out.println(user + "==正在购票...");
        return "购票成功";
    }

    // 购票方法 RestTemplate + Ribbon + Hystrix
//    @RequestMapping(value = "/order", method = RequestMethod.GET)
//    @HystrixCommand(fallbackMethod = "fallbackMethod")
//    public String order(){
//        Integer id = 1;
//        User user = restTemplate.getForObject("http://myshop-user/user" + id, User.class);
//        System.out.println(user + "==正在购票...");
//        return "购票成功";
//    }
//
//    public String fallbackMethod(){
//        return "服务暂时不可以被使用，熔断机制启动";
//    }

}